import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { IView } from "../../mvc/interfaces/IView";
import { FahunModule } from "../../../modules/FahunModule";
import { DataEvent } from "../events/DataEvent";
import { SAmuletSpiritInfo } from "../../../Message/Public/SAmuletSpiritInfo";
import { GameProxy } from "../mvc/GameProxy";
import { EAmuletSpiritOper } from "../../../Message/Game/EAmuletSpiritOper";
import { SMoney } from "../../../Message/Game/SMoney";
import { IFahunModule } from "../../../modules/interfaces/IFahunModule";
type int = number;
//class FahunController
    
    export  class FahunController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.FahunShowTab, this.onFahunShowTabHandler);
            Dispatcher.addEventListener(EventName.FahunInfoUpdate, this.onFahunInfoUpdateHandler);
            Dispatcher.addEventListener(EventName.FahunOper, this.onFahunOperHandler);
            Dispatcher.addEventListener(EventName.FahunOperView, this.onFahunOperViewHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.onPackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onPackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._fahunModule) 
            {
                this._fahunModule = new FahunModule();
                this._fahunModule.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddToStageHandler);
            }
            _view = this._fahunModule;
            return this._fahunModule;
        }

        /* internal  */onAddToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.FahunOperView));
            return;
        }

        /* internal  */onFahunShowTabHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["operIndex"] as int;
            var loc2=arg1.data["itemIndex"] as int;
            view.show();
            this._fahunModule.showToTab(loc1, loc2);
            return;
        }

        /* internal  */onFahunInfoUpdateHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SAmuletSpiritInfo;
            if (isViewShow) 
            {
                this._fahunModule.updateFahunList(cache.fahun.fahunItemInfoArray);
            }
            return;
        }

        /* internal  */onFahunOperHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["index"] as int;
            var loc2=arg1.data["oper"] as int;
            var loc3=arg1.data["materials"] as Array<any>;
            GameProxy.roleProxy.operateAmuletSpirit(loc1, loc2, loc3);
            return;
        }

        /* internal  */onFahunOperViewHandler(arg1: DataEvent): void
        {
            GameProxy.roleProxy.operateAmuletSpirit(1, EAmuletSpiritOper._EAmuletSpiritView, null);
            return;
        }

        /* internal  */onPackChangeHandler(arg1: Object): void
        {
            if (this._fahunModule) 
            {
                this._fahunModule.materialUpdate();
            }
            return;
        }

        /* internal  */onMoneyUpdateHandler(arg1: SMoney=null): void
        {
            if (this._fahunModule) 
            {
                this._fahunModule.moneyUpdate();
            }
            return;
        }

        private /* var */_fahunModule: IFahunModule;
    }
